package com.wechat.order.util.mail;

import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by zzy on 2017/12/27.
 */
@Slf4j
public class EmailUtil {

    /**
     * 发送邮件
     * @param to
     * @param code
     */
    public static void sendMail(String to,String code) throws Exception {
        //1、创建链接对象
        Properties properties=System.getProperties();
        properties.put("mail.smtp.host", "smtp.jzteyao.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");

        if("qq".equals("jzt")){
            MailSSLSocketFactory sf=new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable",true);
            properties.put("mail.smtp.ssl.socketFactory",sf);
        }
        Session session=Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("zhaozeyang@jztey.com","a123456#");
            }
        });
        //2、创建邮件对象
        Message message=new MimeMessage(session);
        //2.1设置发件人
        InternetAddress internetAddress=  new InternetAddress("zhaozeyang@jztey.com");
        message.setFrom(internetAddress);
        //2.2设置收件人
        Address toAddress=new InternetAddress("709796558@qq.com");
        message.setRecipient(Message.RecipientType.TO,toAddress);
        message.setSubject("墨染流云账号激活");
        message.setContent("<h1>墨染流云账号激活：点击链接激活：<h1>" +
                "<h3><a href='http://localhost:8080/registActive?code'"+code+">点击链接</a></h3>","text/html;charset=UTF-8");
        //3、发送激活邮件
        Transport.send(message);
        log.info("完成");
    }
}
